﻿using System;

public class GlobalMembersGame_battle : GlobalMembersGame_attack
{

    internal static LCUI_BOOL list_is_inited;
    internal static LCUI_Queue battle_list = new LCUI_Queue();

    //	//* 初始化游戏帧数控制 
    internal static void GameBattleFrame_Init(BattleFrameStatus_ p_status, int ms_per_frame)
    {
        p_status.state = BattleState. BATTLE_STATE_RUN;
        p_status.one_frame_remain_time = ms_per_frame;
        p_status.prev_frame_start_time = LCUI_GetTickCount();
        LCUISleeper_Create(p_status.wait_continue);
        LCUISleeper_Create(p_status.wait_pause);
    }

    internal static void GameBattleFrame_Init(int battle_id, int ms_per_frame)
    {
        BattleData_ p_battle;

  		p_battle = GameBattle_GetBattle(battle_id);
        BattleFrameStatus_ p_status = p_battle.data_proc_frame;
        p_status.state = BattleState.BATTLE_STATE_RUN;
        p_status.one_frame_remain_time = ms_per_frame;
        p_status.prev_frame_start_time = LCUI_GetTickCount();
        LCUISleeper_Create(p_status.wait_continue);
        LCUISleeper_Create(p_status.wait_pause);
    }

    //	//* 暂停游戏数据帧的更新 
    //	internal  void GameBattleFrame_Pause(BattleFrameStatus_ p_status, LCUI_BOOL need_pause)
    //	{
    //		if(p_status.state == BATTLE_STATE_RUN && need_pause != null)
    //		{
    //			LCUISleeper_BreakSleep(p_status.wait_pause);
    //			p_status.state = BATTLE_STATE_PAUSE;
    //		}
    //		else if(p_status.state == BATTLE_STATE_PAUSE && need_pause == null)
    //		{
    //			LCUISleeper_BreakSleep(p_status.wait_continue);
    //			p_status.state = BATTLE_STATE_RUN;
    //		}
    //	}
    public static int INT_MAX   =    2147483647;
    //	//* 让当前帧停留一定时间 
    internal static void GameBattleFrame_Remain(BattleFrameStatus_ p_status)
    {
        int n_ms;
        int lost_ms;
        int current_time = new int();

        current_time = LCUI_GetTickCount();
        n_ms = (int)(current_time - p_status.prev_frame_start_time);
        if (n_ms > p_status.one_frame_remain_time)
        {
            goto normal_exit;
        }
        n_ms = p_status.one_frame_remain_time - n_ms;
        if (n_ms < 1)
        {
            goto normal_exit;
        }
        LCUISleeper_StartSleep(p_status.wait_pause, n_ms);
        if (p_status.state ==  BattleState. BATTLE_STATE_RUN || p_status.state == BattleState.BATTLE_STATE_QUIT)
        {
            goto normal_exit;
        }

        lost_ms = LCUISleeper_StartSleep(p_status.wait_continue, INT_MAX);
        p_status.prev_frame_start_time += lost_ms;
        return;

        normal_exit:
        p_status.prev_frame_start_time += p_status.one_frame_remain_time;
    }

    internal static void GamePlayerList_Delete(IntPtr arg)
    {
        GamePlayer_ p_player;
        // p_player = (GamePlayer_)arg;
        p_player = (GamePlayer_)System.Runtime.InteropServices.Marshal.PtrToStructure(arg, typeof(GamePlayer_));
        if (p_player.t_action_timeout != -1)
        {
            LCUITimer_Free(p_player.t_action_timeout);
        }
        if (p_player.t_death_timer != -1)
        {
            LCUITimer_Free(p_player.t_death_timer);
        }
        if (p_player.t_rest_timeout != -1)
        {
            LCUITimer_Free(p_player.t_rest_timeout);
        }
        Queue_Destroy(p_player.skills);
        Widget_Destroy(p_player.statusbar);
    }

    internal static void GameBattleList_Delete(IntPtr arg)
    {
        BattleData_ p_battle;
        // p_battle = (BattleData_)arg;
        p_battle = (BattleData_)System.Runtime.InteropServices.Marshal.PtrToStructure(arg, typeof(BattleData_));

        //Widget_Hide(p_battle.scene);
        //LCUIThread_Join(p_battle.th_animation_update, null);
        //GameValueTip_Quit(p_battle.value_tip_proc);
        //Queue_Destroy(p_battle.attack_record);
        //Queue_Destroy(p_battle.player_list);
        //GameObjectLibrary_Destroy(ref p_battle.gameobject_library);
        //GameSapce_Destroy(p_battle.space);
        //Widget_Destroy(p_battle.scene);
    }

    //* 获取指定ID的对战数据 
    internal static  BattleData_ GameBattle_GetBattle(int battle_id)
    {
        int i;
        int n;
        BattleData_ p_data;

        if (list_is_inited == null)
        {
            return null;
        }
        n = Queue_GetTotal(battle_list);
        for (i = 0; i < n; ++i)
        {
            p_data = (BattleData_)Queue_Get(battle_list, i);
            if (p_data == null)
            {
                continue;
            }
            if (p_data.id == battle_id)
            {
                return p_data;
            }
        }
        return null;
    }
    ////C++ TO C# CONVERTER NOTE: This was formerly a static local variable declaration (not allowed in C#):
    //private int battle_id = 0;

    //	//* 新建一个对战，并返回该对战的ID 
    public static int GameBattle_New()
    {
        BattleData_ data = new BattleData_();
        //C++ TO C# CONVERTER NOTE: This static local variable declaration (not allowed in C#) has been moved just prior to the method:
        int battle_id = 0;

        if (list_is_inited == null)
        {
            Queue_Init(battle_list, typeof(BattleData_), GameBattleList_Delete);
            list_is_inited = DefineConstantsGame_battle.TRUE;
        }
        data.id = ++battle_id;
        data.need_sync_camera = DefineConstantsGame_battle.TRUE;
        data.camera_x_padding = 200;
        data.scene_land_pos.x = 0;
        data.scene_land_pos.y = 0;
        data.scene_land_size.w = 0;
        data.scene_land_size.h = 0;
        data.camera_target = null;
        data.space = GlobalMembersGame_space.GameSpace_New();
        //data.scene = Widget_New(null);
        data.scene = Widget_New("scene");
        data.value_tip_proc.is_inited = DefineConstantsGame_battle.FALSE;
        Queue_Init(data.player_list, typeof(GamePlayer_), GamePlayerList_Delete);
        Game_InitAttackRecord(ref data.attack_record);
        GlobalMembersGame_object.GameObjectLibrary_Create(ref data.gameobject_library);
        Queue_Add(battle_list, data);
        return data.id;
    }

    //	//* 退出对战 
    //	public static int GameBattle_Quit(int battle_id)
    //	{
    //		BattleData_ p_battle;

    //		p_battle = GameBattle_GetBattle(battle_id);
    //		if (p_battle == null)
    //		{
    //			return -1;
    //		}
    //		p_battle.animation_frame.state = BATTLE_STATE_QUIT;
    //		p_battle.data_proc_frame.state = BATTLE_STATE_QUIT;
    //		LCUISysEvent_Disconnect(AnonymousEnum.LCUI_KEYUP, p_battle.keyboard_connect_id[0]);
    //		LCUISysEvent_Disconnect(AnonymousEnum.LCUI_KEYDOWN, p_battle.keyboard_connect_id[1]);
    //		return 0;
    //	}

    //	//* 设置是否启用数值提示功能 
    //	public static int GameBattle_SetEnableValueTip(int battle_id, LCUI_BOOL need_enable)
    //	{
    //		BattleData_ p_battle;

    //		p_battle = GameBattle_GetBattle(battle_id);
    //		if (p_battle == null)
    //		{
    //			return -1;
    //		}

    //		if(need_enable == p_battle.value_tip_proc.is_inited)
    //		{
    //			return 1;
    //		}
    //		if(need_enable == null && p_battle.value_tip_proc.is_inited)
    //		{
    //			p_battle.value_tip_proc.is_inited = DefineConstantsGame_battle.FALSE;
    //			GameValueTip_Quit(p_battle.value_tip_proc);
    //		}
    //		p_battle.value_tip_proc.is_inited = DefineConstantsGame_battle.TRUE;
    //		GameValueTip_Init(p_battle.value_tip_proc, p_battle.scene);
    //		return 0;
    //	}

    //	//* 开始对战 
    public static int GameBattle_Start(int battle_id)
    {
        int i;
        int n;
        BattleData_ p_battle;
        GamePlayer_ p_player;

        p_battle = GameBattle_GetBattle(battle_id);
        if (p_battle == null)
        {
            return -1;
        }
        n = Queue_GetTotal(p_battle.player_list);
        // 播放动作动画，并显示游戏角色 
        for (i = 0; i < n; ++i)
        {
            p_player = (GamePlayer_)Queue_Get(p_battle.player_list, i);
            if (!p_player.enable)
            {
                continue;
            }
            // 播放动作动画 
            GlobalMembersGame_object. GameObject_PlayAction(p_player.object_);
            // 显示游戏角色 
            Widget_Show(p_player.object_);
        }
        // 连接键盘事件，以响应按键控制 
        p_battle.keyboard_connect_id[0] = LCUISysEvent_Connect(AnonymousEnum.LCUI_KEYDOWN, GlobalMembersGame_control.GameControl_KeyboardProc, null);
        p_battle.keyboard_connect_id[1] = LCUISysEvent_Connect(AnonymousEnum.LCUI_KEYUP, GlobalMembersGame_control.GameControl_KeyboardProc, null);
        return 0;
    }

    //	//* 设置指定游戏角色的控制键 
    public static int GameBattle_SetPlayerControlKey(int battle_id, int player_id, ControlKey_ ctrlkey)
    {
        GamePlayer_ p_player;
        p_player = GameBattle_GetPlayer(battle_id, player_id);
        if (p_player == null)
        {
            return -1;
        }
        p_player.ctrlkey = ctrlkey;
        return 0;
    }

    //	//* 通过控制键获取该键控制的角色 
    public static GamePlayer_ GameBattle_GetPlayerByControlKey(int key_code)
    {
        int j;
        int i;
        int m;
        int n;
        BattleData_ p_battle;
        GamePlayer_ p_player;

        m = Queue_GetTotal(battle_list);
        for (j = 0; j < m; ++j)
        {
            p_battle = (BattleData_)Queue_Get(battle_list, j);
            if (p_battle == null)
            {
                continue;
            }
            n = Queue_GetTotal(p_battle.player_list);
            for (i = 0; i < n; ++i)
            {
                p_player = (GamePlayer_)Queue_Get(p_battle.player_list, i);
                if (p_player == null)
                {
                    continue;
                }
                if(p_player.id== GameMain.Id)
                if (key_code == p_player.ctrlkey.a_attack || key_code == p_player.ctrlkey.b_attack || key_code == p_player.ctrlkey.defense || key_code == p_player.ctrlkey.left || key_code == p_player.ctrlkey.right || key_code == p_player.ctrlkey.up || key_code == p_player.ctrlkey.down || key_code == p_player.ctrlkey.jump)
                {
                    return p_player;
                }
            }
        }
        return null;
    }

    //* 通过GameObject部件获取游戏玩家数据 
    public static GamePlayer_ GameBattle_GetPlayerByWidget(LCUI_Widget_ gameobject)
    {
        int j;
        int i;
        int m;
        int n;
        BattleData_ p_battle;
        GamePlayer_ p_player;

        m = Queue_GetTotal(battle_list);
        for (j = 0; j < m; ++j)
        {
            p_battle = (BattleData_)Queue_Get(battle_list, j);
            if (p_battle == null)
            {
                continue;
            }
            n = Queue_GetTotal(p_battle.player_list);
            for (i = 0; i < n; ++i)
            {
                p_player = (GamePlayer_)Queue_Get(p_battle.player_list, i);
                if (p_player == null)
                {
                    continue;
                }
                if (p_player.object_ == gameobject)
                {
                    return p_player;
                }
            }
        }
        return null;
    }

    //	//* 获取指定ID的玩家数据 
    public static GamePlayer_ GameBattle_GetPlayer(int battle_id, int player_id)
    {
        int i;
        int n;
        BattleData_ p_data;
        GamePlayer_ player;

        p_data = GameBattle_GetBattle(battle_id);
        if (p_data == null)
        {
            return null;
        }
        n = Queue_GetTotal(p_data.player_list);
        for (i = 0; i < n; ++i)
        {
            player = (GamePlayer_)Queue_Get(p_data.player_list, i);
            if (player == null)
            {
                continue;
            }
            if (player.id == player_id)
            {
                return player;
            }
        }
        return null;
    }

    internal static int GamePlayer_InitAction(GamePlayer_ player, int id)
    {
        int i;
        ActionData_ action;

        player.state = (int)GamePlayerState.STATE_STANCE;
        for (i = 0; i < (int)ActionType.TOTAL_ACTION_NUM; ++i)
        {
            // 载入游戏角色资源 
            action = GlobalMembersGame_action.ActionRes_Load(id, i);
            // 将动作集添加至游戏对象 
            GlobalMembersGame_object. GameObject_AddAction(player.object_, action, i);
        }
        return 0;
    }

    //	//* 响应游戏角色受到的攻击 
    internal static void GamePlayer_ResponseAttack(LCUI_Widget_ widget)
    {
        GamePlayer_ player;
        GamePlayer_ atk_player;
        AttackerInfo_ p_info;
        LCUI_Queue attacker_info;

        player = GameBattle_GetPlayerByWidget(widget);
        if (player == null)
        {
            return;
        }

        attacker_info = GlobalMembersGame_object.GameObject_GetAttackerInfo(widget);
        while (true)
        {
            p_info = (AttackerInfo_)Queue_Get(attacker_info, 0);
            if (p_info == null)
            {
                break;
            }
            atk_player = GameBattle_GetPlayerByWidget(p_info.attacker);
            // 记录本次攻击的信息 
            Game_RecordAttack(atk_player, atk_player.attack_type_name, player, player.state);
            // 删除攻击者记录 
            Queue_Delete(attacker_info, 0);
        }
    }

    //	//* 设置一个玩家 
    public static int GameBattle_SetPlayer(int battle_id, int player_id, int role_id, LCUI_BOOL human_control)
    {
        int i;
        RoleInfo_ p_info;
        BattleData_ p_battle;
        GamePlayer_ player;
        LCUI_Graph_ img_shadow ;
        GamePlayer_ player_data = new GamePlayer_();

        p_battle = GameBattle_GetBattle(battle_id);
        if (p_battle == null)
        {
            return -1;
        }
        player = GameBattle_GetPlayer(battle_id, player_id);
        if (player == null)
        {
            Graph_Init(out img_shadow);
            GlobalMembersGame_control. GamePlayer_Init(player_data);
            GameGraphRes_GetGraph(DefineConstantsGame_battle.MAIN_RES, "shadow", img_shadow);

            player_data.id = player_id;
            player_data.battle_id = battle_id;

            player_data.object_ = GlobalMembersGame_object.GameObject_New(p_battle.space, ref p_battle.gameobject_library);
            player_data.object_.name = player_data.object_.name + player_id;
            GlobalMembersGame_object. GameObject_SetShadow(player_data.object_, img_shadow);
            // 设置响应游戏角色的受攻击信号 
            GlobalMembersGame_object. GameObject_AtUnderAttack(player_data.object_, GamePlayer_ResponseAttack);
            // 将游戏对象添加进对战场景 
            GlobalMembersGame_object. GameObject_AddToContainer(player_data.object_, p_battle.scene);
            // 记录玩家数据 
            Queue_Add(p_battle.player_list, player_data);
            player = GameBattle_GetPlayer(battle_id, player_id);
            if (player == null)
            {
                return -1;
            }
        }

        p_info = GlobalMembersGame_control.Game_GetRoleInfo(role_id);
        if (p_info == null)
        {
            return -2;
        }

        player.type = p_info.type;
        player.role_id = role_id;
        player.property = p_info.property;
        player.human_control = human_control;

        for (i = 0; i < p_info.total_skill && i < DefineConstantsGame_battle.MAX_SKILL_NUM; ++i)
        {
            GlobalMembersGame_skill. GamePlayer_EnableSkill(player, p_info.skills[i]);
            if (string.Compare(p_info.skills[i], DefineConstantsGame_battle.SKILLNAME_MACH_A_ATTACK) == 0)
            {
                GlobalMembersGame_skill. GamePlayer_EnableSkill(player, DefineConstantsGame_battle.SKILLNAME_JUMP_MACH_A_ATTACK);
            }
            else if (string.Compare(p_info.skills[i], DefineConstantsGame_battle.SKILLNAME_MACH_B_ATTACK) == 0)
            {
                GlobalMembersGame_skill. GamePlayer_EnableSkill(player, DefineConstantsGame_battle.SKILLNAME_JUMP_MACH_B_ATTACK);
            }
        }
        // 初始化角色动作动画 
        GamePlayer_InitAction(player, role_id);
        // 根据职业来选择需要启用的特殊技能 
        switch ((GamePlayerType)player.type)
        {
            case GamePlayerType.PLAYER_TYPE_FIGHTER:
                GlobalMembersGame_skill. GamePlayer_EnableSkill(player, DefineConstantsGame_battle.SKILLNAME_HUG_FRONT_PUT);
                GlobalMembersGame_skill. GamePlayer_EnableSkill(player, DefineConstantsGame_battle.SKILLNAME_HUG_BACK_PUT);
                break;
            case GamePlayerType.PLAYER_TYPE_MARTIAL_ARTIST:
                GlobalMembersGame_skill. GamePlayer_EnableSkill(player, DefineConstantsGame_battle.SKILLNAME_KNEEHIT);
                GlobalMembersGame_skill. GamePlayer_EnableSkill(player, DefineConstantsGame_battle.SKILLNAME_ELBOW);
                break;
            case GamePlayerType.PLAYER_TYPE_KUNG_FU:
                GlobalMembersGame_skill. GamePlayer_EnableSkill(player, DefineConstantsGame_battle.SKILLNAME_LIFT_JUMP);
                GlobalMembersGame_skill. GamePlayer_EnableSkill(player, DefineConstantsGame_battle.SKILLNAME_HUG_JUMP);
                break;
            case GamePlayerType.PLAYER_TYPE_JUDO_MASTER:
                GlobalMembersGame_skill. GamePlayer_EnableSkill(player, DefineConstantsGame_battle.SKILLNAME_JUDO);
                GlobalMembersGame_skill. GamePlayer_EnableSkill(player, DefineConstantsGame_battle.SKILLNAME_BACK_JUDO);
                break;
            case GamePlayerType.PLAYER_TYPE_TIGER:
                GlobalMembersGame_skill. GamePlayer_EnableSkill(player, DefineConstantsGame_battle.SKILLNAME_SPIN_DRILL);
                break;
            //C++ TO C# CONVERTER TODO TASK: C# does not allow fall-through from a non-empty 'case':
            default:
                break;
        }
        return 0;
    }

    //	//* 设置是否启用一个玩家 
    public static int GameBattle_EnablePlayer(int battle_id, int player_id, LCUI_BOOL flag)
    {
        GamePlayer_ player;
        player = GameBattle_GetPlayer(battle_id, player_id);
        if (player == null)
        {
            return -1;
        }
        player.enable = flag;
        return 0;
    }

    //	//* 设置指定对战中的玩家是否无敌 
    public static int GameBattlePlayer_SetInvincible(int battle_id, int player_id, LCUI_BOOL flag)
    {
        GamePlayer_ player;
        player = GameBattle_GetPlayer(battle_id, player_id);
        if (player == null)
        {
            return -1;
        }
        player.is_invincible = flag;
        return 0;
    }

    //	//* 设置场景地面参数 
    public static int GameBattle_SetSceneLand(int battle_id, LCUI_Pos_ land_pos, LCUI_Size_ land_size)
    {
        BattleData_ p_battle;
        p_battle = GameBattle_GetBattle(battle_id);
        if (p_battle == null)
        {
            return -1;
        }
        //C++ TO C# CONVERTER WARNING: The following line was determined to be a copy assignment (rather than a reference assignment) - this should be verified and a 'CopyFrom' method should be created if it does not yet exist:
        //ORIGINAL LINE: p_battle->scene_land_pos = land_pos;
        p_battle.scene_land_pos.CopyFrom(land_pos);
        //C++ TO C# CONVERTER WARNING: The following line was determined to be a copy assignment (rather than a reference assignment) - this should be verified and a 'CopyFrom' method should be created if it does not yet exist:
        //ORIGINAL LINE: p_battle->scene_land_size = land_size;
        p_battle.scene_land_size.CopyFrom(land_size);
        // 设置战斗场景的空间边界 
        GlobalMembersGame_space.GameSpace_SetBound(p_battle.space, land_pos.x, land_size.w, land_pos.y, land_size.h);
        return 0;
    }

    //	//* 获取场景地面参数 
    public static int GameBattle_GetSceneLand(int battle_id,out LCUI_Pos_ land_pos,out LCUI_Size_ land_size)
    {
        BattleData_ p_battle;
        land_pos = default(LCUI_Pos_);
        land_size = default(LCUI_Size_);
        p_battle = GameBattle_GetBattle(battle_id);
        if (p_battle == null)
        {
            return -1;
        }
        land_pos.x = p_battle.scene_land_pos.x;
        land_pos.y = p_battle.scene_land_pos.y;
        land_size.w = p_battle.scene_land_size.w;
        land_size.h = p_battle.scene_land_size.h;
        return 0;
    }

    //	//* 设置是否自动同步镜头 
    //	public static int GameBattle_SetAutoSyncCamera(int battle_id, LCUI_BOOL is_true)
    //	{
    //		BattleData_ p_battle;
    //		p_battle = GameBattle_GetBattle(battle_id);
    //		if (p_battle == null)
    //		{
    //			return -1;
    //		}
    //		p_battle.need_sync_camera = is_true;
    //		return 0;
    //	}

    //	//* 设置场景大小 
    public static int GameBattle_SetSceneSize(int battle_id, LCUI_Size_ size)
    {
        BattleData_ p_battle;
        p_battle = GameBattle_GetBattle(battle_id);
        if (p_battle == null)
        {
            return -1;
        }
        //C++ TO C# CONVERTER WARNING: The following line was determined to be a copy assignment (rather than a reference assignment) - this should be verified and a 'CopyFrom' method should be created if it does not yet exist:
        //ORIGINAL LINE: p_battle->scene_size = size;
        p_battle.scene_size.CopyFrom(size);
        Widget_Resize(p_battle.scene, size);
        return 0;
    }

    //	//* 获取场景大小 
    public static int GameBattle_GetSceneSize(int battle_id, LCUI_Size_ size)
    {
        BattleData_ p_battle;
        p_battle = GameBattle_GetBattle(battle_id);
        if (p_battle == null)
        {
            return -1;
        }
        size = p_battle.scene_size;
        return 0;
    }
    public static int LAYOUT_CENTER = 4;
    //	//* 设置场景背景图 
    public static int GameBattle_SetSceneBackground(int battle_id, LCUI_Graph_ stage_img)
    {
        BattleData_ p_battle;
        p_battle = GameBattle_GetBattle(battle_id);
        if (p_battle == null)
        {
            return -1;
        }
        Widget_SetBackgroundImage(p_battle.scene, stage_img);
        //Widget_SetBackgroundLayout(p_battle.scene, LAYOUT_CENTER);
        //Widget_SetBackgroundColor(p_battle.scene, RGB(0, 0, 0));
        //Widget_SetBackgroundTransparent(p_battle.scene, DefineConstantsGame_battle.FALSE);
        return 0;
    }

    //	//* 获取场景 
    public static LCUI_Widget_ GameBattle_GetScene(int battle_id)
    {
        BattleData_ p_battle;
        p_battle = GameBattle_GetBattle(battle_id);
        if (p_battle == null)
        {
            return null;
        }
        return p_battle.scene;
    }

    //* 获取攻击记录 
    public static LCUI_Queue GameBattle_GetAttackRecord(int battle_id)
    {
        BattleData_ p_battle;
        p_battle = GameBattle_GetBattle(battle_id);
        if (p_battle == null)
        {
            return null;
        }
        return p_battle.attack_record;
    }
    ////C++ TO C# CONVERTER NOTE: This was formerly a static local variable declaration (not allowed in C#):
    //private static int speed = 1;

    //	//* 更新场景上的镜头位置，使目标游戏对象处于镜头区域内 
    internal int GameScene_UpdateCamera(LCUI_Widget_ game_scene, LCUI_Widget_ camera_target, int x_padding)
    {
        int target_x;
        int offset_x;
        int wdg_scene_x;
        int screen_width;
        int start_x;
        LCUI_Size_ scene_size;
        //C++ TO C# CONVERTER NOTE: This static local variable declaration (not allowed in C#) has been moved just prior to the method:
        //	static int speed = 1;

        scene_size = Widget_GetSize(game_scene);
        screen_width = LCUIScreen_GetWidth();
        start_x = (screen_width - scene_size.w) / 2;
        target_x = (int)GlobalMembersGame_object.GameObject_GetX(camera_target);
        wdg_scene_x = Widget_GetPos(game_scene).x;
        // 既然大于0，则说明战斗场景小于屏幕区域，无需移动镜头 
        if (wdg_scene_x > 0)
        {
            return 0;
        }
        // 如果目标位置在镜头的左内边框的左边 
        if (wdg_scene_x + target_x < x_padding)
        {
            // 计算出战斗场景的位置 
            offset_x = x_padding - target_x;
            // 判断镜头是否会超出战斗场景的范围 
            if (offset_x > 0)
            {
                offset_x = 0;
            }
            // 得出坐标偏移量 
            offset_x = offset_x - start_x;
            // 将战斗场景向右移动 
            Widget_SetAlign(game_scene, ALIGN_TYPE.ALIGN_BOTTOM_CENTER, Pos(offset_x, -DefineConstantsGame_battle.STATUS_BAR_HEIGHT));
            return 0;
        }
        // 如果目标位置在镜头的右内边框的右边 
        if (wdg_scene_x + target_x > screen_width - x_padding)
        {
            offset_x = screen_width - x_padding - target_x;
            if (offset_x < screen_width - scene_size.w)
            {
                offset_x = screen_width - scene_size.w;
            }
            offset_x = offset_x - start_x;
            Widget_SetAlign(game_scene, ALIGN_TYPE.ALIGN_BOTTOM_CENTER, Pos(offset_x, -DefineConstantsGame_battle.STATUS_BAR_HEIGHT));
        }
        return 0;
    }

    internal  void GameBattle_ProcGameObject(IntPtr arg)
    {
        BattleData_ p_battle;
        //p_battle = (BattleData_)arg;
        p_battle= (BattleData_)System.Runtime.InteropServices.Marshal.PtrToStructure(arg, typeof(BattleData_));
        // 初始化游戏动画帧处理 
        GameBattleFrame_Init(p_battle.animation_frame, 20);
        while (p_battle.animation_frame.state !=BattleState. BATTLE_STATE_QUIT)
        {
            GlobalMembersGame_space.GameSpace_Step(p_battle.space);
            GlobalMembersGame_object. GameObjectLibrary_UpdateAction(ref p_battle.gameobject_library);
            GlobalMembersGame_object. GameObjectLibrary_DispatchEvent(ref p_battle.gameobject_library);
            GameBattleFrame_Remain(p_battle.animation_frame);
        }
        LCUIThread_Exit(null);
    }

    internal static void GameBattle_ProcGameObject(int battle_id)
    {
        // 初始化游戏动画帧处理 
        BattleData_ p_battle;
        p_battle = GameBattle_GetBattle(battle_id);
        //GameBattleFrame_Init(p_battle.animation_frame, 20);
        if (p_battle.animation_frame.state != BattleState.BATTLE_STATE_QUIT)
        {
            GlobalMembersGame_space.GameSpace_Step(p_battle.space);
            GlobalMembersGame_object.GameObjectLibrary_UpdateAction(ref p_battle.gameobject_library);
            GlobalMembersGame_object.GameObjectLibrary_DispatchEvent(ref p_battle.gameobject_library);
            GameBattleFrame_Remain(p_battle.animation_frame);
        }
        LCUIThread_Exit(null);
    }

    //	//* 设置是否暂停对战 
    //	public int GameBattle_Pause(int battle_id, LCUI_BOOL need_pause)
    //	{
    //		int i;
    //		int n;
    //		BattleData_ p_battle;
    //		GamePlayer_ p_player;

    //		p_battle = GameBattle_GetBattle(battle_id);
    //		if (p_battle == null)
    //		{
    //			return -1;
    //		}
    //		GameBattleFrame_Pause(p_battle.data_proc_frame, need_pause);
    //		GameBattleFrame_Pause(p_battle.animation_frame, need_pause);
    //		n = Queue_GetTotal(p_battle.player_list);
    //		for(i =0; i<n; ++i)
    //		{
    //			p_player = (GamePlayer_)Queue_Get(p_battle.player_list, i);
    //			if (p_player == null)
    //			{
    //				continue;
    //			}
    //			GamePlayer_SetPause(p_player, need_pause);
    //		}
    //		return 0;
    //	}

    //	//* 进入循环 
    public int GameBattle_Loop(int battle_id)
    {
        LCUI_Thread th = new LCUI_Thread();
        int i;
        int n;
        int n_found;
        BattleData_ p_battle;
        GamePlayer_ p_player;

        p_battle = GameBattle_GetBattle(battle_id);
        if (p_battle == null)
        {
            return -1;
        }
        // 初始化游戏AI 
        //GameAI_Init();
        // 创建一个线程，用于处理游戏对象的数据更新 
       // LCUIThread_Create(th, GameBattle_ProcGameObject, p_battle);
        // 初始化游戏数据帧处理 
        GameBattleFrame_Init(p_battle.data_proc_frame, 20);
        // 循环更新游戏数据 
        while (p_battle.data_proc_frame.state != BattleState.BATTLE_STATE_QUIT)
        {
            n = Queue_GetTotal(p_battle.player_list);
            for (n_found = 0, i = 0; i < n; ++i)
            {
                p_player = (GamePlayer_)Queue_Get(p_battle.player_list, i);
                if (p_player == null)
                {
                    continue;
                }
                if (!p_player.enable || !p_player.local_control)
                {
                    continue;
                }
                ++n_found;
                // 如果该游戏玩家不是由人类控制的 
                if (!p_player.human_control)
                {
                    //GameAI_Control(p_battle.id, p_player.id);
                }
                else
                {
                    GlobalMembersGame_control.GamePlayer_SyncKeyControl(p_player);
                }
                GlobalMembersGame_control.GamePlayer_SyncData(p_player);
                Widget_Update(p_player.object_);
                // 将第一个有效的游戏角色设置为镜头焦点 
                if (n_found == 1)
                {
                    p_battle.camera_target = p_player.object_;
                }
            }
            // 如果需要更新镜头 
            if (p_battle.need_sync_camera && p_battle.camera_target)
            {
                GameScene_UpdateCamera(p_battle.scene, p_battle.camera_target, p_battle.camera_x_padding);
            }
            // 处理攻击 
            Game_ProcAttack(ref p_battle.attack_record, p_battle.value_tip_proc);
            // 本帧数据处理完后，停留一段时间 
            GameBattleFrame_Remain(p_battle.data_proc_frame);
        }

        Queue_Lock(battle_list);
        n = Queue_GetTotal(battle_list);
        for (i = 0; i < n; ++i)
        {
            p_battle = (BattleData_)Queue_Get(battle_list, i);
            if (p_battle != null && p_battle.id == battle_id)
            {
                Queue_Delete(battle_list, i);
                break;
            }
        }
        Queue_Unlock(battle_list);
        return 0;
    }
}
//// ****************************************************************************
//// * Punch And Kick -- a simple 2D Fighting Game.
//// *
//// * Copyright (C) 2013 by
//// * Liu Chao
//// *
//// * This file is part of the Punch And Kick project, and may only be used,
//// * modified, and distributed under the terms of the GPLv2.
//// *
//// * (GPLv2 is abbreviation of GNU General Public License Version 2)
//// *
//// * By continuing to use, modify, or distribute this file you indicate that you
//// * have read the license and understand and accept it fully.
//// *
//// * The Punch And Kick project is distributed in the hope that it will be
//// * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
//// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GPL v2 for
//// * more details.
//// *
//// * You should have received a copy of the GPLv2 along with this file. It is
//// * usually in the LICENSE.TXT file, If not, see <http://www.gnu.org/licenses/>.
//// * **************************************************************************

//// ****************************************************************************
//// * Punch And Kick -- 一个普通的2D格斗游戏
//// *
//// * 版权所有 (C) 2013 归属于
//// * 刘超
//// *
//// * 这个文件是 Punch And Kick 项目的一部分，并且只可以根据GPLv2许可协议来使用、
//// * 更改和发布。
//// *
//// * (GPLv2 是 GNU通用公共许可证第二版 的英文缩写)
//// *
//// * 继续使用、修改或发布本文件，表明您已经阅读并完全理解和接受这个许可协议。
//// *
//// * Punch And Kick 项目是基于技术交流目的而加以散布的，但不负任何担保责任，例
//// * 如：稳定性、可玩性、趣味性等的担保，甚至没有适销性或特定用途的隐含担保，详
//// * 情请参照GPLv2许可协议。
//// *
//// * 您应已收到附随于本文件的GPLv2许可协议的副本，它通常在LICENSE.TXT文件中，如果
//// * 没有，请查看：<http://www.gnu.org/licenses/>.
//// * **************************************************************************

//// 游戏对战模块




////C++ TO C# CONVERTER TODO TASK: The following statement was not recognized, possibly due to an unrecognized macro:
//	//BATTLE_STATE_RUN, BATTLE_STATE_PAUSE, BATTLE_STATE_QUIT


public enum BattleState
{
    BATTLE_STATE_RUN,
    BATTLE_STATE_PAUSE,
    BATTLE_STATE_QUIT
};

public class BattleFrameStatus_
    {
        public BattleState state;
        public LCUI_Sleeper wait_pause = new LCUI_Sleeper();
        public LCUI_Sleeper wait_continue = new LCUI_Sleeper();
        public int one_frame_remain_time;
        public int prev_frame_start_time = new int();
    }

    public class BattleData_
    {
        public int id;
        public LCUI_Queue player_list = new LCUI_Queue();
        public LCUI_Widget_ scene;
        public LCUI_Size_ scene_size = new LCUI_Size_();
        public LCUI_Pos_ scene_land_pos = new LCUI_Pos_();
        public LCUI_Size_ scene_land_size = new LCUI_Size_();
        public LCUI_BOOL need_sync_camera =true;
        public LCUI_Widget_ camera_target;
        public int camera_x_padding;
        public GameSpaceData_ space;
        public LCUI_Queue gameobject_library = new LCUI_Queue();
        public LCUI_Queue attack_record = new LCUI_Queue();
        public BattleFrameStatus_ animation_frame = new BattleFrameStatus_();
        public BattleFrameStatus_ data_proc_frame = new BattleFrameStatus_();
        public ValueTipData_ value_tip_proc = new ValueTipData_();
        public LCUI_Thread th_animation_update = new LCUI_Thread();
        public int[] keyboard_connect_id = new int[2];
    }


public class FrameControlData_
{
	public int one_frame_remain_time;
	public int prev_frame_start_time = new int();
}
